# handler_mini.py
#
#

""" exec handler.  just return the results in a <div>. """

import time

starttime = time.time()

## gozerlib imports

from gozerlib.web.bot import WebBot
from gozerlib.web.event import WebEvent
from gozerlib.utils.generic import fromenc, toenc, getversion
from gozerlib.utils.xmpp import stripped
from gozerlib.plugins import plugs
from gozerlib.utils.auth import checkuser
from gozerlib.utils.web import execbox, commandbox, mini, loginurl, closer
from gozerlib.persist.persist import Persist
from gozerlib.utils.exception import handle_exception
from gozerlib.boot import boot

## google imports

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.api import users as gusers

## simplejson import

from waveapi.simplejson import loads

## basic imports

import wsgiref.handlers
import sys
import time
import types
import os
import logging

logging.warn(getversion('MINI'))

boot()
bot = WebBot('cmndmini')
plugs.loadall()

class ExecHandler(webapp.RequestHandler):

    """ the bots exec command dispatcher. """

    def get(self):

        """ this is where the command get disaptched. """

        global starttime

        if starttime:
            self.response.starttime = starttime
            starttime = 0
        else:
            self.response.starttime = time.time()

        (userhost, user, u, nick) = checkuser(self.response, self.request)
        logging.debug("EXEC incoming: %s" % self.request.remote_addr)
        event = WebEvent().parse(self.response, self.request)
        mini(self.response, {'who': event.userhost, 'onload': '', 'loginurl': loginurl(self.response) })
        
        if not event.txt:
            event.txt = 'welcome'

        event.nick = nick
        event.userhost = userhost
        event.auth = userhost
        event.stripped = stripped(userhost)
        event.channel = stripped(userhost)
        event.bot = bot

        try:
            event.bot.doevent(event)

        except Exception, ex:
            handle_exception(event)

        closer(self.response)

    def post(self):

        """ this is where the command get disaptched. """

        global starttime

        if starttime:
            self.response.starttime = starttime
            starttime = 0
        else:
            self.response.starttime = time.time()

        (userhost, user, u, nick) = checkuser(self.response, self.request)
        logging.debug("EXEC incoming: %s" % self.request.remote_addr)
        event = WebEvent().parse(self.response, self.request)
        mini(self.response, {'who': event.userhost, 'onload': 'putFocus(0,0);', 'loginurl': loginurl(self.response) })
        
        if not event.txt:
            event.txt = 'help'

        event.nick = nick
        event.userhost = userhost
        event.auth = userhost
        event.stripped = stripped(userhost)
        event.channel = stripped(userhost)
        event.bot = bot

        try:
            event.bot.doevent(event)

        except Exception, ex:
            handle_exception(event)

        closer(self.response)

# the application 

application = webapp.WSGIApplication([('/mini', ExecHandler),
                                      ('/mini/', ExecHandler)],
                                      debug=True)

def main():
    global bot
    global application
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
